﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.Timers;

namespace DonwloadBingImage
{
    public partial class DonwloadBingImage : ServiceBase
    {
        public Timer MyTimer { get; set; }

        public DonwloadBingImage()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this.MyTimer = new System.Timers.Timer();
            ((System.ComponentModel.ISupportInitialize)(this.MyTimer)).BeginInit();
            this.MyTimer.Enabled = true;
            this.MyTimer.Interval = System.Convert.ToDouble(int.Parse(Properties.Settings.Default.MinutesToRun) * 60 * 1000);
            this.MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
            ((System.ComponentModel.ISupportInitialize)(this.MyTimer)).EndInit();
            BingImage.DownLoadImages();
        }

        protected override void OnStop()
        {
        }

        void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            MyTimer.Enabled = false;
            BingImage.DownLoadImages();
            MyTimer.Enabled = true;
        }
    }
}
